using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TankGame
{
    public class Game1 : Game
    {
        //MENU

        //STARTING MENU

        private const int NUMBER_OF_BUTTONS = 5,
                          START_BUTTON_INDEX = 0,
                          TRAINING_BUTTON_INDEX = 1,
                          MULTIPLAYER_BUTTON_INDEX = 2,
                          OPTIONS_BUTTON_INDEX = 3,
                          EXIT_BUTTON_INDEX = 4,
                          BUTTON_HEIGHT = 50,
                          BUTTON_WIDTH = 120;

        private const int NUMBER_OF_SBUTTONS = 2,
                          DEFAULT_BUTTON_INDEX = 0,
                          SHERMAN_BUTTON_INDEX = 1,
                          BUTTON_SHEIGHT = 80,
                          BUTTON_SWIDTH = 80;

        private readonly List<Bullet> Bullets = new List<Bullet>();
        private readonly List<Bullet> MachineBullets = new List<Bullet>();
        private readonly List<Bullet> SBullets = new List<Bullet>();

        private readonly Color[] Sbutton_color = new Color[NUMBER_OF_SBUTTONS];
        private readonly Rectangle[] Sbutton_rectangle = new Rectangle[NUMBER_OF_SBUTTONS];
        private readonly CState[] Sbutton_state = new CState[NUMBER_OF_SBUTTONS];
        private readonly Texture2D[] Sbutton_texture = new Texture2D[NUMBER_OF_SBUTTONS];
        private readonly double[] Sbutton_timer = new double[NUMBER_OF_SBUTTONS];
        private readonly Color[] button_color = new Color[NUMBER_OF_BUTTONS];
        private readonly Rectangle[] button_rectangle = new Rectangle[NUMBER_OF_BUTTONS];
        private readonly BState[] button_state = new BState[NUMBER_OF_BUTTONS];
        private readonly Texture2D[] button_texture = new Texture2D[NUMBER_OF_BUTTONS];
        private readonly double[] button_timer = new double[NUMBER_OF_BUTTONS];
        private readonly GraphicsDeviceManager graphics;
        private readonly Random random = new Random();
        private Vector2 BulletPosition;
        private Vector2 MachineBulletPosition;
        public DateTime MachineNextBullet;
        public TimeSpan MachinebulletDelay = new TimeSpan(0, 0, 0, 0, 100);
        private Vector2 SBulletPosition;
        private Vector2 STurretPosition;
        public TimeSpan SbulletDelay = new TimeSpan(0, 0, 0, 0, 100);
        private Boolean SdrawExplosion;
        private Boolean Semitlocationused;
        private double Sframe_time;
        //mouse pressed and mouse just pressed
        private bool Smpressed;
        //mouse location in window
        private int Smx, Smy;
        public DateTime SnextBullet;
        private MouseState Sold2State;
        private MouseState SoldState;
        private bool Sprev_mpressed;
        private Vector2 TurretPosition;
        private BoundingBox bb1, bb2;

        //Textures
        private Texture2D bg;
        private Song boom;

        private Texture2D bullet;

        public TimeSpan bulletDelay = new TimeSpan(0, 0, 0, 0, 100);
        private float bulletx;
        private float bullety;
        private SpriteFont defaultfont;
        private Texture2D defaulttankfull;

        private float defaulttankx = 1150;
        private Texture2D dirt;

        //terrain
        private int dirt1x;
        private int dirt1y;
        private int dirt2x = 500;
        private int dirt2y;
        private int dirt3x;
        private int dirt3y = 500;
        private int dirt4x = 500;
        private int dirt4y = 500;
        public Boolean dontStartMenu;
        private Boolean drawExplosion;
        private ParticleEngine5 dustCreator;
        private Boolean dustCreatorLoaded;
        private Boolean emitlocationused;
        private ParticleEngine4 flameThrowerEngine;
        private Boolean flameThrowerEngineLoaded;
        private double frame_time;
        public Boolean gameStarted;
        public Boolean inTankSelectScreen;
        private Vector3 maxv1;
        private Vector3 maxv2;

        //Angles

        private float mbulletx;
        private float mbullety;
        private Vector3 minv1;
        private Vector3 minv2;
        private bool mpressed;
        private int mx, my;
        public DateTime nextBullet;

        private MouseState old2State;
        private MouseState oldState;

        private List<Texture2D> pEngine1List;
        private ParticleEngine3 pEngine3;
        private List<Texture2D> pEngine3List;
        private Boolean pEngine3loaded;
        private bool prev_mpressed;
        private float randomy;
        private float randomy2;
        private BoundingBox sbb1, sbb2;
        private float sbulletx;
        private float sbullety;
        private Texture2D shermantank;
        private Texture2D shermantankfull;
        private Texture2D shermantankloop;
        private Texture2D shermantanktop;
        private float shermantankx = -100;
        private List<Texture2D> smallBulletsList;
        private Vector3 smaxv1;
        private Vector3 smaxv2;

        //Numbers

        //Fonts
        //DEFAULT TANK

        //SHERMAN TANK
        private Vector3 sminv1;
        private Vector3 sminv2;
        private Texture2D snow;
        private SpriteBatch spriteBatch;
        private float stangle;
        private int stpx1 = 400;
        private int stpy1 = 400;
        private float sttangle;
        private float sttlangle;
        private int sttx;
        private int stty;
        private int sturretr = 170;
        private double stx = 100;
        private double sty = 100;
        private float tangle;
        private Texture2D tank;
        private Texture2D tankloop;
        private Texture2D tanktop;
        private Texture2D targetpractice;
        private int tpx1 = 400;
        private int tpy1 = 400;
        private float ttangle;
        private float ttlangle;
        private int ttx;
        private int tty;
        private int turretr = 130;
        private double tx = 100;
        private double ty = 100;
        private Boolean usingDefaultTank;
        private Boolean usingShermanTank;
        //IF GAME STARTED

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            Window.Title = "Tank Game";

            IsMouseVisible = true;

            graphics.PreferredBackBufferWidth = 1000;
            graphics.PreferredBackBufferHeight = 900;
            graphics.ApplyChanges();
        }

        protected override void Initialize()
        {
            base.Initialize();

            generateNewY();
            generateNewY2();

            int x = Window.ClientBounds.Width/2 - BUTTON_WIDTH/2;
            int y = Window.ClientBounds.Height/2 - NUMBER_OF_BUTTONS/2*BUTTON_HEIGHT -
                    (NUMBER_OF_BUTTONS%2)*BUTTON_HEIGHT/2;
            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {
                button_state[i] = BState.UP;
                button_color[i] = Color.White;
                button_timer[i] = 0.0;
                button_rectangle[i] = new Rectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
                y += BUTTON_HEIGHT;
            }

            int Sx = Window.ClientBounds.Width/2 - BUTTON_SWIDTH/2;
            int Sy = Window.ClientBounds.Height/2 - NUMBER_OF_SBUTTONS/2*BUTTON_SHEIGHT -
                     (NUMBER_OF_SBUTTONS%2)*BUTTON_SHEIGHT/2;
            for (int i = 0; i < NUMBER_OF_SBUTTONS; i++)
            {
                Sbutton_state[i] = CState.SUP;
                Sbutton_color[i] = Color.White;
                Sbutton_timer[i] = 0.0;
                Sbutton_rectangle[i] = new Rectangle(Sx, Sy, BUTTON_SWIDTH, BUTTON_SHEIGHT);
                Sy += BUTTON_SHEIGHT;
            }
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            bg = Content.Load<Texture2D>("menu/bg");

            shermantankfull = Content.Load<Texture2D>("menu/shermantank");
            defaulttankfull = Content.Load<Texture2D>("menu/defaulttank");

            button_texture[START_BUTTON_INDEX] = Content.Load<Texture2D>("button/start");
            button_texture[TRAINING_BUTTON_INDEX] = Content.Load<Texture2D>("button/training");
            button_texture[MULTIPLAYER_BUTTON_INDEX] = Content.Load<Texture2D>("button/multiplayer");
            button_texture[OPTIONS_BUTTON_INDEX] = Content.Load<Texture2D>("button/options");
            button_texture[EXIT_BUTTON_INDEX] = Content.Load<Texture2D>("button/exit");

            Sbutton_texture[DEFAULT_BUTTON_INDEX] = Content.Load<Texture2D>("button/defaulttankbutton");
            Sbutton_texture[SHERMAN_BUTTON_INDEX] = Content.Load<Texture2D>("button/shermantankbutton");

            //default tank
            tank = Content.Load<Texture2D>("tank/default/tank");
            tanktop = Content.Load<Texture2D>("tank/default/tanktop");
            tankloop = Content.Load<Texture2D>("tank/default/tankloop");
            bullet = Content.Load<Texture2D>("tank/bullet");
            //default tank

            //sherman tank
            shermantank = Content.Load<Texture2D>("tank/sherman/onderkant");
            shermantanktop = Content.Load<Texture2D>("tank/sherman/turret");
            shermantankloop = Content.Load<Texture2D>("tank/sherman/tankloop");
            //sherman tank

            //terrain
            dirt = Content.Load<Texture2D>("terrain/6481");
            snow = Content.Load<Texture2D>("terrain/snow");
            targetpractice = Content.Load<Texture2D>("terrain/targetpractice");
            //terain

            //particles
            pEngine1List = new List<Texture2D>();
            pEngine1List.Add(Content.Load<Texture2D>("particles/smoke"));

            pEngine3List = new List<Texture2D>();
            pEngine3List.Add(Content.Load<Texture2D>("particles/smoke"));

            smallBulletsList = new List<Texture2D>();
            smallBulletsList.Add(Content.Load<Texture2D>("particles/smallsmoke"));

            pEngine3 = new ParticleEngine3(pEngine3List, new Vector2(targetpractice.Bounds.X, targetpractice.Bounds.Y));
            pEngine3loaded = true;

            flameThrowerEngine = new ParticleEngine4(pEngine3List, new Vector2(-100, -100));
            flameThrowerEngineLoaded = true;

            dustCreator = new ParticleEngine5(pEngine1List, new Vector2(-100, -100));
            dustCreatorLoaded = true;

            //particles

            defaultfont = Content.Load<SpriteFont>("Default");

            //sounds
            boom = Content.Load<Song>("tank/boom");
            //sounds
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (gameStarted == false && dontStartMenu == false)
            {
                frame_time = gameTime.ElapsedGameTime.Milliseconds/1000.0;

                MouseState mouse_state = Mouse.GetState();
                mx = mouse_state.X;
                my = mouse_state.Y;
                prev_mpressed = mpressed;
                mpressed = mouse_state.LeftButton == ButtonState.Pressed;

                update_buttons();
            }

            if (inTankSelectScreen)
            {
                Sframe_time = gameTime.ElapsedGameTime.Milliseconds/1000.0;

                MouseState Smouse_state = Mouse.GetState();
                Smx = Smouse_state.X;
                Smy = Smouse_state.Y;
                Sprev_mpressed = Smpressed;
                Smpressed = Smouse_state.LeftButton == ButtonState.Pressed;

                Supdate_buttons();
            }

            if (gameStarted)
            {
                KeyboardState state = Keyboard.GetState();
                MouseState mouseState = Mouse.GetState();

                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    Exit();

                if (state.IsKeyDown(Keys.Escape))
                    gameStarted = false;
                inTankSelectScreen = false;
                dontStartMenu = false;

                //DEFAULT TANK
                if (usingDefaultTank)
                {
                    if (state.IsKeyDown(Keys.A))
                    {
                        tangle -= 0.01f;
                    }
                    if (state.IsKeyDown(Keys.D))
                    {
                        tangle += 0.01f;
                    }
                    if (state.IsKeyDown(Keys.W))
                    {
                        tx += Math.Cos(tangle - 1.570796327)*1;
                        ty += Math.Sin(tangle - 1.570796327)*1;
                    }

                    if (state.IsKeyDown(Keys.S))
                    {
                        tx -= Math.Cos(tangle - 1.570796327)*1;
                        ty -= Math.Sin(tangle - 1.570796327)*1;
                    }

                    ttx = (int) tx + 70;
                    tty = (int) ty + 90;

                    TurretPosition = new Vector2(ttx, tty);

                    MouseState newState = Mouse.GetState();

                    MouseState new2State = Mouse.GetState();

                    ttangle =
                        (float) Math.Atan2(TurretPosition.Y - Mouse.GetState().Y, TurretPosition.X - Mouse.GetState().X) -
                        MathHelper.ToRadians(180.0f);
                    var bulletsToRemove = new List<Bullet>();

                    foreach (Bullet B in Bullets)
                    {
                        B.Update(gameTime);

                        minv1 = new Vector3(tpx1 - 40, tpy1 - 10, 0); //tp
                        minv2 = new Vector3(B.Position.X, B.Position.Y, 0); //bullet
                        maxv1 = new Vector3(tpx1 + 40, tpy1 + 10, 0); //tp
                        maxv2 = new Vector3(B.Position.X + bullet.Width, B.Position.Y + bullet.Height, 0); //bullet

                        bb1.Min = minv1;
                        bb2.Min = minv2;

                        bb1.Max = maxv1;
                        bb2.Max = maxv2;

                        if (bb1.Intersects(bb2))
                        {
                            float impactpointx = B.Position.X;
                            float impactpointy = B.Position.Y;
                            if (emitlocationused == false)
                            {
                                pEngine3.EmitterLocation = new Vector2(impactpointx, impactpointy);
                                emitlocationused = true;
                            }
                            pEngine3.createExplosion();
                            drawExplosion = true;
                            bulletsToRemove.Add(B);
                        }
                    }

                    for (int i = 0; i < bulletsToRemove.Count; i++)
                    {
                        Bullets.Remove(bulletsToRemove[i]);
                    }

                    if (newState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released &&
                        nextBullet <= DateTime.Now)
                    {
                        emitlocationused = false;

                        BulletPosition = new Vector2(tanktop.Height*(float) Math.Cos(ttangle),
                                                     tanktop.Height*(float) Math.Sin(ttangle));
                        BulletPosition += TurretPosition;

                        bulletx = (float) Math.Cos(ttangle)*1000;
                        bullety = (float) Math.Sin(ttangle)*1000;

                        var B = new Bullet(BulletPosition, new Vector2(bulletx, bullety), 4.0f,
                                           ttangle + MathHelper.ToRadians(90.0f), pEngine1List);

                        Bullets.Add(B);

                        nextBullet = DateTime.Now + bulletDelay;
                    }

                    var MbulletsToRemove = new List<Bullet>();

                    foreach (Bullet MB in MachineBullets)
                    {
                        MB.Update(gameTime);

                        minv1 = new Vector3(tpx1 - 40, tpy1 - 10, 0); //tp
                        minv2 = new Vector3(MB.Position.X, MB.Position.Y, 0); //bullet
                        maxv1 = new Vector3(tpx1 + 40, tpy1 + 10, 0); //tp
                        maxv2 = new Vector3(MB.Position.X + bullet.Width, MB.Position.Y + bullet.Height, 0); //bullet

                        bb1.Min = minv1;
                        bb2.Min = minv2;

                        bb1.Max = maxv1;
                        bb2.Max = maxv2;

                        if (MB.Life <= 0)
                        {
                            MbulletsToRemove.Add(MB);
                        }

                        if (bb1.Intersects(bb2))
                        {
                            float MBimpactpointx = MB.Position.X;
                            float MBimpactpointy = MB.Position.Y;
                            pEngine3.EmitterLocation = new Vector2(MBimpactpointx, MBimpactpointy);
                            pEngine3.createMachineBulletExplosion();
                            drawExplosion = true;
                            MbulletsToRemove.Add(MB);
                        }
                    }

                    for (int i = 0; i < MbulletsToRemove.Count; i++)
                    {
                        MachineBullets.Remove(MbulletsToRemove[i]);
                    }

                    if (mouseState.RightButton == ButtonState.Pressed && MachineNextBullet <= DateTime.Now)
                    {
                        MachineBulletPosition = new Vector2(tanktop.Height*(float) Math.Cos(ttangle),
                                                            tanktop.Height*(float) Math.Sin(ttangle));
                        MachineBulletPosition += TurretPosition;

                        mbulletx = (float) Math.Cos(ttangle)*1000;
                        mbullety = (float) Math.Sin(ttangle)*1000;

                        var MB = new Bullet(MachineBulletPosition, new Vector2(mbulletx, mbullety), 4.0f,
                                            ttangle + MathHelper.ToRadians(90.0f), smallBulletsList);

                        MachineBullets.Add(MB);

                        MachineNextBullet = DateTime.Now + MachinebulletDelay;
                    }

                    oldState = newState;

                    old2State = new2State;


                    ttangle =
                        (float) Math.Atan2(TurretPosition.Y - Mouse.GetState().Y, TurretPosition.X - Mouse.GetState().X) -
                        MathHelper.ToRadians(90.0f);
                }
                //DEFAULT TANK

                //SHERMAN TANK
                if (usingShermanTank)
                {
                    if (state.IsKeyDown(Keys.A))
                        stangle -= 0.01f;

                    if (state.IsKeyDown(Keys.D))
                        stangle += 0.01f;

                    if (state.IsKeyDown(Keys.W))
                    {
                        stx += Math.Cos(stangle - 1.570796327)*1;
                        sty += Math.Sin(stangle - 1.570796327)*1;
                    }

                    if (state.IsKeyDown(Keys.S))
                    {
                        stx -= Math.Cos(stangle - 1.570796327)*1;
                        sty -= Math.Sin(stangle - 1.570796327)*1;
                    }

                    //THE FUCKING X POSITION OF THE FUCKING SHERMAN TANK FUCK
                    sttx = (int) stx + 70;
                    stty = (int) sty + 90;

                    STurretPosition = new Vector2(sttx, stty);

                    MouseState SnewState = Mouse.GetState();

                    MouseState Snew2State = Mouse.GetState();

                    sttangle =
                        (float)
                        Math.Atan2(STurretPosition.Y - Mouse.GetState().Y, STurretPosition.X - Mouse.GetState().X) -
                        MathHelper.ToRadians(180.0f);

                    var SbulletsToRemove = new List<Bullet>();

                    foreach (Bullet SB in SBullets)
                    {
                        SB.Update(gameTime);

                        sminv1 = new Vector3(stpx1 - 40, stpy1 - 10, 0); //tp
                        sminv2 = new Vector3(SB.Position.X, SB.Position.Y, 0); //bullet
                        smaxv1 = new Vector3(stpx1 + 40, stpy1 + 10, 0); //tp
                        smaxv2 = new Vector3(SB.Position.X + bullet.Width, SB.Position.Y + bullet.Height, 0); //bullet

                        sbb1.Min = sminv1;
                        sbb2.Min = sminv2;

                        sbb1.Max = smaxv1;
                        sbb2.Max = smaxv2;

                        if (sbb1.Intersects(sbb2))
                        {
                            float Simpactpointx = SB.Position.X;
                            float Simpactpointy = SB.Position.Y;
                            if (Semitlocationused == false)
                            {
                                pEngine3.EmitterLocation = new Vector2(Simpactpointx, Simpactpointy);
                                Semitlocationused = true;
                            }
                            pEngine3.createExplosion();
                            SdrawExplosion = true;
                            SbulletsToRemove.Add(SB);
                        }
                    }

                    for (int i = 0; i < SbulletsToRemove.Count; i++)
                    {
                        SBullets.Remove(SbulletsToRemove[i]);
                    }

                    if (SnewState.LeftButton == ButtonState.Pressed && SoldState.LeftButton == ButtonState.Released &&
                        SnextBullet <= DateTime.Now)
                    {
                        Semitlocationused = false;

                        SBulletPosition = new Vector2(shermantanktop.Height*(float) Math.Cos(sttangle),
                                                      shermantanktop.Height*(float) Math.Sin(sttangle));
                        SBulletPosition += STurretPosition;

                        sbulletx = (float) Math.Cos(sttangle)*1500;
                        sbullety = (float) Math.Sin(sttangle)*1500;

                        var SB = new Bullet(SBulletPosition, new Vector2(sbulletx, sbullety), 4.0f,
                                            ttangle + MathHelper.ToRadians(90.0f), pEngine1List);

                        SBullets.Add(SB);

                        SnextBullet = DateTime.Now + SbulletDelay;
                    }

                    SoldState = SnewState;

                    Sold2State = Snew2State;

                    flameThrowerEngine.EmitterLocation = new Vector2(sttx, stty);
                    flameThrowerEngine.angle = sttangle;
                    flameThrowerEngine.Update();

                    if (mouseState.RightButton == ButtonState.Pressed)
                    {
                        flameThrowerEngine.createFlames();
                    }

                    sttangle =
                        (float)
                        Math.Atan2(STurretPosition.Y - Mouse.GetState().Y, STurretPosition.X - Mouse.GetState().X) -
                        MathHelper.ToRadians(90.0f);
                }
                //SHERMAN TANK

                if (pEngine3loaded)
                {
                    pEngine3.Update();
                }

                if (dustCreatorLoaded)
                {
                    dustCreator.Update();
                }
            }

            base.Update(gameTime);
        }

        public void generateNewY()
        {
            randomy = random.Next(0, 450);
        }

        public void generateNewY2()
        {
            randomy2 = random.Next(450, 900);
        }

        //IMAGE PRESSING

        private Boolean hit_image_alpha(Rectangle rect, Texture2D tex, int x, int y)
        {
            return hit_image_alpha(0, 0, tex, tex.Width*(x - rect.X)/
                                              rect.Width, tex.Height*(y - rect.Y)/rect.Height);
        }

        private Boolean hit_image_alpha(float tx, float ty, Texture2D tex, int x, int y)
        {
            if (hit_image(tx, ty, tex, x, y))
            {
                var data = new uint[tex.Width*tex.Height];
                tex.GetData(data);
                if ((x - (int) tx) + (y - (int) ty)*
                                     tex.Width < tex.Width*tex.Height)
                {
                    return ((data[
                        (x - (int) tx) + (y - (int) ty)*tex.Width
                                 ] &
                             0xFF000000) >> 24) > 20;
                }
            }
            return false;
        }

        private Boolean hit_image(float tx, float ty, Texture2D tex, int x, int y)
        {
            return (x >= tx &&
                    x <= tx + tex.Width &&
                    y >= ty &&
                    y <= ty + tex.Height);
        }

        private void update_buttons()
        {
            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {
                if (hit_image_alpha(
                    button_rectangle[i], button_texture[i], mx, my))
                {
                    button_timer[i] = 0.0;
                    if (mpressed)
                    {
                        button_state[i] = BState.DOWN;
                        button_color[i] = Color.Orange;
                    }
                    else if (!mpressed && prev_mpressed)
                    {
                        if (button_state[i] == BState.DOWN)
                        {
                            button_state[i] = BState.JUST_RELEASED;
                        }
                    }
                    else
                    {
                        button_state[i] = BState.HOVER;
                        button_color[i] = Color.LightBlue;
                    }
                }
                else
                {
                    button_state[i] = BState.UP;
                    if (button_timer[i] > 0)
                    {
                        button_timer[i] = button_timer[i] - frame_time;
                    }
                    else
                    {
                        button_color[i] = Color.White;
                    }
                }

                if (button_state[i] == BState.JUST_RELEASED)
                {
                    take_action_on_button(i);
                }
            }
        }

        //SECOND BUTTONS
        private void Supdate_buttons()
        {
            for (int i = 0; i < NUMBER_OF_SBUTTONS; i++)
            {
                if (hit_image_alpha(
                    Sbutton_rectangle[i], Sbutton_texture[i], Smx, Smy))
                {
                    Sbutton_timer[i] = 0.0;
                    if (Smpressed)
                    {
                        Sbutton_state[i] = CState.SDOWN;
                        Sbutton_color[i] = Color.Orange;
                    }
                    else if (!Smpressed && Sprev_mpressed)
                    {
                        if (Sbutton_state[i] == CState.SDOWN)
                        {
                            Sbutton_state[i] = CState.SJUST_RELEASED;
                        }
                    }
                    else
                    {
                        Sbutton_state[i] = CState.SHOVER;
                        Sbutton_color[i] = Color.LightBlue;
                    }
                }
                else
                {
                    Sbutton_state[i] = CState.SUP;
                    if (Sbutton_timer[i] > 0)
                    {
                        Sbutton_timer[i] = Sbutton_timer[i] - Sframe_time;
                    }
                    else
                    {
                        Sbutton_color[i] = Color.White;
                    }
                }

                if (Sbutton_state[i] == CState.SJUST_RELEASED)
                {
                    Stake_action_on_button(i);
                }
            }
        }

        // Logic for each button click goes here
        private void take_action_on_button(int i)
        {
            //take action corresponding to which button was clicked
            switch (i)
            {
                case START_BUTTON_INDEX:
                    inTankSelectScreen = true;
                    gameStarted = false;
                    dontStartMenu = true;
                    break;
                case TRAINING_BUTTON_INDEX:
                    break;
                case MULTIPLAYER_BUTTON_INDEX:
                    break;
                case OPTIONS_BUTTON_INDEX:
                    break;
                case EXIT_BUTTON_INDEX:
                    Exit();
                    break;
                default:
                    break;
            }
        }

        //IMAGE PRESSING

        private void Stake_action_on_button(int i)
        {
            //take action corresponding to which button was clicked
            switch (i)
            {
                case DEFAULT_BUTTON_INDEX:
                    gameStarted = true;
                    dontStartMenu = true;
                    inTankSelectScreen = false;
                    usingDefaultTank = true;
                    usingShermanTank = false;
                    break;
                case SHERMAN_BUTTON_INDEX:
                    gameStarted = true;
                    dontStartMenu = true;
                    inTankSelectScreen = false;
                    usingShermanTank = true;
                    usingDefaultTank = false;
                    break;
                default:
                    break;
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            if (gameStarted == false && dontStartMenu == false)
            {
                spriteBatch.Begin();

                spriteBatch.Draw(dirt, new Vector2(dirt1x, dirt1y), Color.White);
                spriteBatch.Draw(dirt, new Vector2(dirt2x, dirt2y), Color.White);
                spriteBatch.Draw(dirt, new Vector2(dirt3x, dirt3y), Color.White);
                spriteBatch.Draw(dirt, new Vector2(dirt4x, dirt4y), Color.White);

                spriteBatch.Draw(bg, new Rectangle(0, 0, bg.Width, bg.Height), Color.White);

                shermantankx += 2;

                if (shermantankx >= 1150)
                {
                    generateNewY();
                    shermantankx = -100;
                }

                defaulttankx -= 1;

                if (defaulttankx <= -100)
                {
                    generateNewY2();
                    defaulttankx = 1150;
                }

                spriteBatch.Draw(shermantankfull, new Vector2(shermantankx, randomy),
                                 new Rectangle(0, 0, shermantankfull.Width, shermantankfull.Height), Color.White,
                                 MathHelper.ToRadians(90f),
                                 new Vector2(shermantankfull.Width/2, shermantankfull.Height/2), 1.0f,
                                 SpriteEffects.None, 1);

                spriteBatch.Draw(defaulttankfull, new Vector2(defaulttankx, randomy2),
                                 new Rectangle(0, 0, defaulttankfull.Width, defaulttankfull.Height), Color.White, 0f,
                                 new Vector2(defaulttankfull.Width/2, defaulttankfull.Height/2), 1.0f,
                                 SpriteEffects.None, 1);

                for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
                {
                    spriteBatch.Draw(button_texture[i], button_rectangle[i], button_color[i]);
                }

                spriteBatch.End();
            }

            if (inTankSelectScreen)
            {
                spriteBatch.Begin();

                //draw snow
                spriteBatch.Draw(snow, new Vector2(dirt1x, dirt1y), Color.White);
                spriteBatch.Draw(snow, new Vector2(dirt2x, dirt2y), Color.White);
                spriteBatch.Draw(snow, new Vector2(dirt3x, dirt3y), Color.White);
                spriteBatch.Draw(snow, new Vector2(dirt4x, dirt4y), Color.White);

                for (int i = 0; i < NUMBER_OF_SBUTTONS; i++)
                {
                    spriteBatch.Draw(Sbutton_texture[i], Sbutton_rectangle[i], Sbutton_color[i]);
                }

                spriteBatch.End();
            }

            if (gameStarted)
            {
                if (usingDefaultTank)
                {
                    var tlocation = new Vector2((float) tx + tank.Width/2, (float) ty + tank.Height/2);
                    var tsourceRectangle = new Rectangle(0, 0, tank.Width, tank.Height);
                    var torigin = new Vector2(tank.Width/2, tank.Height/2);

                    var ttlocation = new Vector2(ttx, tty);
                    var ttsourceRectangle = new Rectangle(0, 0, tanktop.Width, tanktop.Height);
                    var ttorigin = new Vector2(tanktop.Width/2 + 10, tanktop.Height/2);
                    ttangle = (float) Math.Atan2(tty - Mouse.GetState().Y, ttx - Mouse.GetState().X);

                    float location1 = (tanktop.Height)*(float) Math.Cos(ttangle);
                    float location2 = (tanktop.Height)*(float) Math.Sin(ttangle);
                    location1 += ttx;
                    location2 += tty;

                    var ttllocation = new Vector2(location1, location2);
                    var ttlsourceRectangle = new Rectangle(0, 0, tankloop.Width, tankloop.Height);
                    var ttlorigin = new Vector2(tankloop.Width + turretr, tankloop.Height/2 + 1);
                    ttlangle = (float) Math.Atan2(tty - Mouse.GetState().Y, ttx - Mouse.GetState().X);

                    spriteBatch.Begin();

                    //draw dirt
                    spriteBatch.Draw(dirt, new Vector2(dirt1x, dirt1y), Color.White);
                    spriteBatch.Draw(dirt, new Vector2(dirt2x, dirt2y), Color.White);
                    spriteBatch.Draw(dirt, new Vector2(dirt3x, dirt3y), Color.White);
                    spriteBatch.Draw(dirt, new Vector2(dirt4x, dirt4y), Color.White);

                    //tank

                    spriteBatch.Draw(tank, tlocation, tsourceRectangle, Color.White, tangle, torigin, 1.0f,
                                     SpriteEffects.None, 1);

                    spriteBatch.Draw(tanktop, ttlocation, ttsourceRectangle, Color.White, ttangle, ttorigin, 1.0f,
                                     SpriteEffects.None, 1);

                    foreach (Bullet B in Bullets)
                    {
                        spriteBatch.Draw(bullet, B.Position, null, Color.White, B.Direction,
                                         new Vector2(bullet.Width/2, bullet.Height/2), 1.0f, SpriteEffects.None, 0);
                        B.Emitter.Draw(spriteBatch);
                    }

                    foreach (Bullet MB in MachineBullets)
                    {
                        spriteBatch.Draw(bullet, MB.Position, null, Color.White, MB.Direction,
                                         new Vector2(bullet.Width/2, bullet.Height/2), 1.0f, SpriteEffects.None, 0);
                        MB.Emitter.Draw(spriteBatch);
                    }

                    spriteBatch.Draw(tankloop, ttllocation, ttlsourceRectangle, Color.White, ttlangle, ttlorigin, 1.0f,
                                     SpriteEffects.None, 1);

                    if (dustCreatorLoaded)
                    {
                        dustCreator.Draw(spriteBatch);
                    }

                    spriteBatch.End();
                }

                if (usingShermanTank)
                {
                    //shermantank, shermantanktop, shermantankloop

                    var stlocation = new Vector2((float) stx + shermantank.Width/2, (float) sty + shermantank.Height/2);
                    var stsourceRectangle = new Rectangle(0, 0, shermantank.Width, shermantank.Height);
                    var storigin = new Vector2(shermantank.Width/2, shermantank.Height/2);

                    var sttlocation = new Vector2(sttx, stty);
                    var sttsourceRectangle = new Rectangle(0, 0, shermantanktop.Width, shermantanktop.Height);
                    var sttorigin = new Vector2(shermantanktop.Width/2 + 1, shermantanktop.Height/2);
                    sttangle = (float) Math.Atan2(stty - Mouse.GetState().Y, sttx - Mouse.GetState().X);

                    float slocation1 = (shermantanktop.Height)*(float) Math.Cos(sttangle);
                    float slocation2 = (shermantanktop.Height)*(float) Math.Sin(sttangle);
                    slocation1 += sttx;
                    slocation2 += stty;

                    var sttllocation = new Vector2(slocation1, slocation2);
                    var sttlsourceRectangle = new Rectangle(0, 0, shermantankloop.Width, shermantankloop.Height);
                    var sttlorigin = new Vector2(shermantankloop.Width + sturretr, shermantankloop.Height/2 + 1);
                    sttlangle = (float) Math.Atan2(stty - Mouse.GetState().Y, sttx - Mouse.GetState().X);

                    spriteBatch.Begin();

                    //draw dirt
                    spriteBatch.Draw(dirt, new Vector2(dirt1x, dirt1y), Color.White);
                    spriteBatch.Draw(dirt, new Vector2(dirt2x, dirt2y), Color.White);
                    spriteBatch.Draw(dirt, new Vector2(dirt3x, dirt3y), Color.White);
                    spriteBatch.Draw(dirt, new Vector2(dirt4x, dirt4y), Color.White);

                    spriteBatch.Draw(shermantank, stlocation, stsourceRectangle, Color.White, stangle, storigin, 1.0f,
                                     SpriteEffects.None, 1);

                    foreach (Bullet SB in SBullets)
                    {
                        spriteBatch.Draw(bullet, SB.Position, null, Color.White, SB.Direction,
                                         new Vector2(bullet.Width/2, bullet.Height/2), 1.0f, SpriteEffects.None, 0);
                        SB.Emitter.Draw(spriteBatch);
                    }

                    if (flameThrowerEngineLoaded)
                    {
                        flameThrowerEngine.Draw(spriteBatch);
                    }

                    spriteBatch.Draw(shermantankloop, sttllocation, sttlsourceRectangle, Color.White, sttlangle,
                                     sttlorigin, 1.0f, SpriteEffects.None, 1);


                    spriteBatch.Draw(shermantanktop, sttlocation, sttsourceRectangle, Color.White, sttangle, sttorigin,
                                     1.0f, SpriteEffects.None, 1);

                    spriteBatch.End();
                }

                //tank

                spriteBatch.Begin();

                spriteBatch.Draw(targetpractice, new Rectangle(400, 400, targetpractice.Width, targetpractice.Height),
                                 null, Color.White, MathHelper.ToRadians(0f),
                                 new Vector2(targetpractice.Width/2, targetpractice.Height/2), SpriteEffects.None, 0);

                spriteBatch.DrawString(defaultfont, "v: 0.0.3", new Vector2(0, -7), Color.Black);

                //pEngine1.Draw(spriteBatch);

                if (drawExplosion)
                {
                    pEngine3.Draw(spriteBatch);
                }

                if (SdrawExplosion)
                {
                    pEngine3.Draw(spriteBatch);
                }

                spriteBatch.End();
            }


            base.Draw(gameTime);
        }

        #region Nested type: BState

        private enum BState
        {
            HOVER,
            UP,
            JUST_RELEASED,
            DOWN
        }

        #endregion

        #region Nested type: CState

        private enum CState
        {
            SHOVER,
            SUP,
            SJUST_RELEASED,
            SDOWN
        }

        #endregion
    }
}